package chess;

import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 * This class creates a JPanel, based on given input, that holds a group of {@link javax.swing.JRadioButton}s.
 * This class allows any view controller in any program to easily add in a group of {@link javax.swing.JRadioButton}s in a single JPanel.
 * @author Ben
 */
@SuppressWarnings("serial")
public class RadioGroup extends JPanel 
{
	/**
	 * This is the listener that listens to each individual {@link javax.swing.JRadioButton}.
	 */
	private ActionListener listener;
	
	/**
	 * This is an ArrayList<String> of labels, which contain the 
	 * title for each individual JRadioButton.
	 */
	private ArrayList<String> labels;
	
	/**
	 * This is an ArrayList<String> of commandStrings, which contain the 
	 * ActionCommands for each individual JRadioButton.
	 */
	private ArrayList<String> commandStrings;
	
	/**
	 * This is an ArrayList<JRadioButton> of radioButtons, which holds all the individual 
	 * JRadioButtons to be added into the group, and later the panel
	 */
	private ArrayList<JRadioButton> radioButtons;
	
	/**
	 * This constructor creates a new RadioGroup with the given input.
	 * @param listener - listener added to each JRadioButton.
	 * @param labels - labels for each JRadioButton.
	 * @param commandStrings - Action command strings for each JRadioButton.
	 * @param defaultSelected - boolean determining whether the first JRadioButton in the group is to start preselected or not.
	 */
	public RadioGroup(ActionListener listener, ArrayList<String> labels, ArrayList<String> commandStrings, boolean defaultSelected)
	{
		radioButtons = new ArrayList<JRadioButton>();
		this.listener = listener;
		this.labels = labels;
		this.commandStrings = commandStrings;
		setLayout(new GridLayout(0,1));
		ButtonGroup group = new ButtonGroup();
		
		for(int x = 0; x < commandStrings.size(); x++)
		{
			JRadioButton radio = new JRadioButton(labels.get(x));
			radioButtons.add(radio);
			if(x==0 && defaultSelected)
				radio.setSelected(true);
			radio.setActionCommand(commandStrings.get(x));
			radio.addActionListener(listener);
			group.add(radio);
			add(radio);
		}
	}
	
	public ActionListener getlistener() {
		return listener;
	}
	public ArrayList<String> getLabels() {
		return labels;
	}
	public ArrayList<String> getCommandStrings() {
		return commandStrings;
	}

	protected ArrayList<JRadioButton> getRadioButtons() {
		return radioButtons;
	}
}
